package com.leopardcat.firewall;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import com.leopardcat.firewall.FirewallService;

public class FirewallPre extends PreferenceActivity {
	public static String TAG = "FirewallPre";
	
	/**Called when activity created */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.firewall_pre);
		
		CheckBoxPreference isRunningCheck = (CheckBoxPreference) findPreference("sms_filter_key");
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
		
		settings.edit().putBoolean("sms_filter_key", FirewallService.mIsRunning).commit();
		isRunningCheck.setChecked(FirewallService.mIsRunning);

	}

	/** Called when preference clicked*/
    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    	Log.d(TAG, "PreferenceClick key:" + preference.getKey());
    	
    	if( preference.getKey() != null && preference.getKey().equals("sms_filter_key") ) {
    		CheckBoxPreference checkboxPre = (CheckBoxPreference) preference;
    		
    		if (checkboxPre.isChecked() ) {
    			Toast.makeText(getApplicationContext(), "SMS Firewall start...", Toast.LENGTH_SHORT).show();
    		} else {
    			Toast.makeText(getApplicationContext(), "SMS Firewall stop...", Toast.LENGTH_SHORT).show();
    		}
    		
        	Intent service = new Intent(this, FirewallService.class);
    		
    		if( checkboxPre.isChecked() ) {
        		getApplicationContext().startService(service);
    		}
    		else {
    			getApplicationContext().stopService(service);
    		}
    	}
    	else if ( preference.getKey() != null && preference.getKey().equals("sms_black_list") ) {
    		Intent intent = new Intent();
    		intent.setClass(this, SMSBlackListActivity.class);
    		startActivity(intent);
    	}
    	else if ( preference.getKey() != null && preference.getKey().equals("about_me") ) {
    		new AlertDialog.Builder(this)
    		.setTitle("About CallFirewall")
    		.setMessage("cater's call firewall.")
    		.show();
    	}
    	return true;
    }
}
